package com.lzit.smart_canteen.controller;

import com.lzit.smart_canteen.domain.CartItem;
import com.lzit.smart_canteen.domain.Order;
import com.lzit.smart_canteen.domain.OrderItem;
import com.lzit.smart_canteen.domain.User;
import com.lzit.smart_canteen.service.FoodService;
import com.lzit.smart_canteen.service.ItemService;
import com.lzit.smart_canteen.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/mobile")
public class MobilePageController {
    @Autowired
    private FoodService foodService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private OrderService orderService;

    @GetMapping("/toMobile")
    public String toMobile() {
        return "/mobile/mobile_template";
    }

    @GetMapping("/toUser")
    public String toUser(Model model) {
        model.addAttribute("pageName", "我的");
        return "/mobile/user";
    }

    @GetMapping("/toIndex")
    public String toIndex(Model model) {
        model.addAttribute("pageName", "首页");
        return "/mobile/index";
    }

    @GetMapping("/toCart")
    public String toCart(Model model, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("userInfo");
        if (user != null) {
            List<CartItem> cart = itemService.queryCart(user.getId());
            if (cart.isEmpty()){
                model.addAttribute("cart",null);
            } else {
                model.addAttribute("cart", cart);
            }
        }
        model.addAttribute("pageName", "购物车");
        return "/mobile/cart";
    }

    @GetMapping("/toToken")
    public String toToken(HttpServletRequest request,Model model) {
        User user = (User) request.getSession().getAttribute("userInfo");
        if (user != null) {
            Integer uid = user.getId();
            List<List<OrderItem>> totalItems = new ArrayList<>();
            List<Order> orders = orderService.findOrdersByUidAndStatus(uid,"process");
            if (!orders.isEmpty()) {
                for (Order order:orders){
                    List<OrderItem> items = itemService.findOrderItemsByOid(order.getOid());
                    items.removeIf(item -> Objects.equals(item.getStatus(), "OK"));
//                    System.out.println("items="+items);
                    totalItems.add(items);
                }
//                System.out.println(totalItems);
            }
            List<Order> orders_f = orderService.findOrdersByUidAndStatus(uid,"finish");
            if (!orders_f.isEmpty()) {
                for (Order order:orders_f){
                    List<OrderItem> items = itemService.findOrderItemsByOid(order.getOid());
                    items.removeIf(item -> Objects.equals(item.getStatus(), "OK"));
//                    System.out.println("items="+items);
                    totalItems.add(items);
                }
//                System.out.println(totalItems);
            }
            if (totalItems.isEmpty()){
                model.addAttribute("totalItems",null);
            } else {
                model.addAttribute("totalItems",totalItems);
            }
        }
        model.addAttribute("pageName", "令牌");
        return "/mobile/token";
    }

    @GetMapping("/toMenu")
    public String toMenu(Model model) {
        model.addAttribute("windows", foodService.queryWindow());
        model.addAttribute("pageName", "菜单");
        return "/mobile/menu";
    }

}
